<!DOCTYPE HTML>
<html>
<head>
<?php
	include("components/helper.php");
	include("components/metadata.php");
?>
<title>40Towers - The Papers</title>
<link rel="stylesheet" href="css/global.css" />
<link rel="stylesheet" href="css/thepapers.css" />
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<?php
	include("components/analytics.php");
	include("components/database.php");	
?>
</head>
<body>

<div id="page">
    <?php
		include("components/header.php");
		include("components/nav.php");
	?>
    
    <section class="group" id="papers">
        <h1>The Papers</h1>
        <div class="papers-logo">
        	<img src="img/torquay_news_logo.gif" width="300" height="75" alt="Torquay newspaper logo" />
        </div>
        <?php 
			if ($result = $mysqli->query("SELECT * FROM NewsItem ORDER BY postDate DESC")) { 
				while($obj = $result->fetch_object()){ 
					$id = $obj->id;
					$title = $obj->title; 
					$bodyCopy = substr($obj->bodyCopy, 0, strpos($obj->bodyCopy, ' ', 256)); 
					$postDate = $obj->postDate;
					
					$datetime = str_replace(" ", "T", $postDate);
					
					$sourceUrl = $obj->sourceUrl;
					
					$imageUrl = "img/missing_image.gif";
					
					if ($result2 = $mysqli->query("SELECT * FROM image WHERE id=".$obj->imageId )) { 
						while($obj2 = $result2->fetch_object()){ 
							$imageUrl = "img/". $obj2->url;
						}
					}
		?>
        <article>
        	<h2><a href='news_item.php?id=<?php echo $id; ?>'><?php echo replaceSpeechMarks($title); ?></a></h2>
            <?php if ( $imageUrl != "img/missing_image.gif" ) { ?>
               	
			<?php } ?>
        	<p><?php echo replaceSpeechMarks($bodyCopy); ?></p>
            <?php if ( $sourceUrl != "#" ) { ?>
            	<footer>
                	<p>Posted on <time datetime="<?php echo $datetime; ?>"><?php echo date('l jS \of F Y', strtotime($postDate) ); ?></time> |&nbsp;<a href='news_item.php?id=<?php echo $id; ?>'>Full&nbsp;Story</a>&nbsp;|&nbsp;<a href="<?php echo $sourceUrl; ?>" target="_blank">Source</a></p>
                </footer>
                <hr>
                <?php if ( rand(1,3)==1 ) { ?>
                <img class="banner" src="img/local_ads_<?php echo rand(0,6); ?>.gif" alt="Fawlty Towers Ad" />
                <hr>
                <?php } ?>
			<?php } ?>
            
        </article>
        <?php
				} 
			}
		?>
    </section>
    
    <?php
		include("components/rightBanners.php");
	?>
    
</div>
<?php
	include("components/footer.php");
?>
</body>
</html>
